package com.michaelbulava.TFS.ui.dialogs;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * Created by mbulava on 6/15/2015.
 */
public abstract class BaseDialog extends DialogWrapper {
    protected JComponent contentPane;
    private boolean initialized = false;
    //private JComponent baseControl;

//    protected BaseDialog(){
//        this(IdeModalityType.IDE);
//    }

    protected BaseDialog(IdeModalityType ideModalityType){
        this(null, ideModalityType);
    }

    protected BaseDialog(@Nullable Project project){
        this(project, IdeModalityType.IDE);
    }

    protected BaseDialog(@Nullable Project project, IdeModalityType ideModalityType){
        super(project, true, ideModalityType);
        myInit();
    }

    protected BaseDialog(@NotNull Component parent){
        super(parent, true);
        myInit();
    }

    protected void myInit(){
        setModal(true);
        this.createContentPane();
        setAutoAdjustable(true);
        //createCenterPanel();
        setTitle(dialogTitle());
    }

    @Override
    protected void init(){
        super.init();
        initialized = true;
        //pack();
        postInit();
    }

    protected void postInit(){

    }

    @Override
    public final void show(){
        if (!initialized)
            init();
        super.show();
    }

    @Override
    public boolean showAndGet(){
        if (!initialized)
            init();
        return super.showAndGet();
    }

    @Override
    public AsyncResult<Boolean> showAndGetOk(){
        if (!initialized)
            init();
        return super.showAndGetOk();
    }

    @Override
    protected Action[] createActions() {
        return new Action[0];
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getDimensionServiceKey() {
        return getClass().getName();
    }

    protected Action[] defaultActions(){
        return this.getHelpId() == null?(SystemInfo.isMac?new Action[]{this.getCancelAction(), this.getOKAction()}:new Action[]{this.getOKAction(), this.getCancelAction()}):(SystemInfo.isMac?new Action[]{this.getHelpAction(), this.getCancelAction(), this.getOKAction()}:new Action[]{this.getOKAction(), this.getCancelAction(), this.getHelpAction()});
    }

    public JButton createJButtonForAction(Action action) {
        return super.createJButtonForAction(action);
    }

//    @Override
//    public void show() {
//        createAndShow();
//    }
//
//    @Override
//    public boolean showAndGet() {
//        createAndShow();
//        return isOK();
//    }

//    private final int createAndShow() {
//        baseControl = createControl();
//        //if (baseControl instanceof IBaseDialogControl)
//        //((IBaseDialogControl) baseControl).setBaseDialogActionListener(this);
//
//        if (!(baseControl instanceof  JPanel)){
//            contentPane.add(baseControl,
//                    new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_BOTH,
//                            GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
//                            GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
//                            null, null, null, 0, false));
//        } else {
//
//        }
//
//        super.show();
//
//        return getExitCode();
//    }


    /**
     * Factory method. It creates panel with dialog options. Options panel is located at the center of the dialog's
     * content pane. The implementation can return <code>null</code> value. In this case there will be no options
     * panel.
     *
     * @return center panel
     */
    @Nullable
    @Override
    protected final JComponent createCenterPanel() {
        if (contentPane == null)
            contentPane = createControl();
        return contentPane;
    }


//    private void setupUI(){
//        contentPane = new JPanel();
//        contentPane.setLayout(new GridLayoutManager(1, 1, new Insets(10, 10, 10, 10), -1, -1));
//    }



    protected abstract JComponent createControl();

    protected abstract String dialogTitle();

    protected JComponent getRootComponent(){
        return contentPane;
    }
}
